package xyz.thetbw.autocode

import java.util.*

/**
 * 版本信息
 */
class Version private constructor(){
    lateinit var commitIdAbbrev: String
    lateinit var commitIdFull: String
    lateinit var commitMessage: String
    lateinit var commitTime: String
    lateinit var gitBranch: String
    lateinit var buildVersion: String
    lateinit var buildTime: String

    companion object {
        fun load(): Version? {
            val versionInfo = Version()
            loadGitInfo(versionInfo)
            return versionInfo
        }

        private fun loadGitInfo(version: Version){
            val input = this.javaClass.classLoader.getResourceAsStream(Constants.GIT_PROPERTIES_FILE_NAME) ?: return
            val properties = Properties()
            properties.load(input)
            input.close()
            version.commitIdAbbrev = properties.getProperty("git.commit.id.abbrev")
            version.commitIdFull = properties.getProperty("git.commit.id.full")
            version.commitMessage = properties.getProperty("git.commit.message.full")
            version.commitTime = properties.getProperty("git.commit.time")
            version.buildTime = properties.getProperty("git.build.time")
            version.gitBranch = properties.getProperty("git.branch")
            version.buildVersion = properties.getProperty("git.build.version")
        }
    }
}